// Quantitative Angiogram Analysis System
//
// Copyright ?Ping Zou, 2006
// sg71.cherub@gmail.com

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Printing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;

using WeifenLuo.WinFormsUI;
using eee.Sheffield.PZ.Imaging;
using eee.Sheffield.PZ.Imaging.ImageIO;

namespace eee.Sheffield.PZ.QAAS
{
    /// <summary>
    /// main form for QAAS
    /// </summary>
    public partial class MainForm : Form
    {
        #region Fields
        private static string configurationFile = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "QAASConfiguration.xml");
        private static string dockPanelConfigFile = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "DockManager.config");
        private static string logFile = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "QAASLogFil.xml");
        private Configuration configuration = new Configuration();
        //private int unnamedImageDoc = 0;
        public LogForm logForm;
        public HistogramForm histogramForm;
        public FilterForm filterForm;
        #endregion

        #region Constructor
        public MainForm()
        {
            InitializeComponent();
            // load configuration
            configuration.Load(configurationFile);
            // init menus
            autoSaveLogToolStripMenuItem.Checked = configuration.autoSaveLog;
            showLogFormToolStripMenuItem.Checked = configuration.showLogForm;
            showFilterFormToolStripMenuItem.Checked = configuration.showFilterForm;
            showHistogramFormToolStripMenuItem.Checked = configuration.showHistogramForm;
            // dock log form
            ShowLogForm();
            // dock histogram form
            ShowHistogramForm();
            // dock filter form
            ShowFilterForm();                       
        }
        #endregion        

        #region MainForm Events
        /// <summary>
        /// form closed
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            // save log
            if (configuration.autoSaveLog)
                logForm.Save(logFile);
            // save QAAS configuration
            configuration.Save(configurationFile);
        }

        /// <summary>
        /// form load
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MainForm_Load(object sender, EventArgs e)
        {
            // test methods
            TestMethod();
        }

        private void dockPanelMainForm_ActiveContentChanged(object sender, EventArgs e)
        {
            ImageDoc imageDoc = _dockPanelMainForm.ActiveContent as ImageDoc;
            if (imageDoc != null && imageDoc.ImageFileName != null)
            {
                // change current directory
                Directory.SetCurrentDirectory(Path.GetDirectoryName(imageDoc.ImageFileName));
                // link imageDoc to filter control in the filter form
                if (filterForm.FilterControl != null)
                {
                    ((FilterControl)filterForm.FilterControl).ImageDoc = (ImageDoc)_dockPanelMainForm.ActiveContent;
                    filterForm.FilterControl.Reload();
                }
            }
        }

        #endregion

        #region Main Form methods
        public void ShowImage(Bitmap image, string caption)
        {
            try
            {
                // create image document
                ImageDoc newImageDoc = new ImageDoc(image);
                newImageDoc.TabText = caption;
                if (newImageDoc != null)
                {
                    string logText = "Image <" + newImageDoc.TabText + "> is shown.";
                    Log(logText);
                    newImageDoc.Show(_dockPanelMainForm);
                    newImageDoc.Focus();
                }
            }
            catch (ApplicationException e)
            {
                String errorText = "QAAS::ShowImage(), " + e.Message;
                MessageBox.Show(errorText, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                // log
                Log(errorText);
            }            
        }        
        #endregion    
    }
}